﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Routing;

namespace Developer.Service
{
    public class CreateDirectory
    {
        public void CreatePath(string path)
        {
            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);
        }
    }

    public class GetUrl
    {
        public string GetUrlStr()
        {
            WebClient WC = new WebClient();
            Uri url = System.Web.HttpContext.Current.Request.Url;
            //防止在正式环境中没有加80端口号报错
            switch (url.Port)
            {
                case 0:
                    string s = string.Format("http://{0}/", url.Host);
                    return s;
                case 443:
                    string s_443 = string.Format("https://{0}/", url.Host);
                    return s_443;
                case 80:
                    string s_80 = string.Format("http://{0}:{1}/", url.Host, url.Port);
                    return s_80;
                default:
                    string sDefault = string.Format("http://{0}/", url.Host);
                    return sDefault;
            }


        }

        /// <summary>
        /// 判断CurrentPage传入参数
        /// </summary>
        /// <param name="currentPage">当前页面值</param>
        /// <returns>过滤后的CurrentPage</returns>
        public int CheckParamCurrentPage(int currentPage)
        {
            int NewCurrentPage = currentPage;
            //判断是否为数字，如果是将currentPage赋值给NewCurrentPage
            //否则currentPage = 1
            if (int.TryParse(currentPage.ToString(), out NewCurrentPage))
            {
                //判断当前页面是否大于0
                //如果大于0,currentPage = pageCount
                //小于0，当前页为1
                if (currentPage > 0)
                {
                    return currentPage;
                }
                else
                {
                    return currentPage = 1;
                }
            }
            else
            {
                return currentPage = 1;
            }
        }

        /// <summary>
        /// 判断pageSize参数
        /// </summary>
        /// <param name="pageSize">页面大小，每页显示数据个数</param>
        /// <param name="DefaultPageSize">系统默认每页大小</param>
        /// <returns>参数过滤后的pageSize</returns>
        public int CheckPageSize(int pageSize, int DefaultPageSize)
        {
            int NewPageSize = DefaultPageSize;
            if (int.TryParse(pageSize.ToString(), out NewPageSize))
            {
                if (pageSize > 0 && pageSize < DefaultPageSize)
                {
                    return pageSize;
                }
                else
                {
                    return DefaultPageSize;
                }
            }
            else
            {
                return DefaultPageSize;
            }
        }

        #region ImgeHandler

        public class ImageHandler : IHttpHandler
        {
            public ImageHandler(RequestContext context)
            {
                ProcessRequest(context);
            }

            private static void ProcessRequest(RequestContext requestContext)
            {
                var response = requestContext.HttpContext.Response;
                var request = requestContext.HttpContext.Request;
                var server = requestContext.HttpContext.Server;
                var validRequestFile = requestContext.RouteData.Values["filename"].ToString();
                const string invalidRequestFile = "thief.gif";
                var path = server.MapPath("~/graphics/");

                response.Clear();
                response.ContentType = GetContentType(request.Url.ToString());

                if (request.ServerVariables["HTTP_REFERER"] != null &&
                    request.ServerVariables["HTTP_REFERER"].Contains("mikesdotnetting.com"))
                {
                    response.TransmitFile(path + validRequestFile);
                }
                else
                {
                    response.TransmitFile(path + invalidRequestFile);
                }
                response.End();
            }

            private static string GetContentType(string url)
            {
                switch (Path.GetExtension(url))
                {
                    case ".gif":
                        return "Image/gif";
                    case ".jpg":
                        return "Image/jpeg";
                    case ".png":
                        return "Image/png";
                    default:
                        break;
                }
                return null;
            }

            public void ProcessRequest(HttpContext context)
            {
            }

            public bool IsReusable
            {
                get { return false; }
            }
        }

        #endregion

        /// <summary>
        /// 转换JSON时间
        /// </summary>
        /// <param name="milliTime">json时间数据</param>
        /// <returns>转换后的数据</returns>

        public DateTime ConvertTime(string milliTime)
        {
            long timeTricks = new DateTime(1970, 1, 1).Ticks + long.Parse(milliTime) * 10000;
            return new DateTime(timeTricks);
        }

        /// <summary>
        /// 将时间戳转换为日期类型，并格式化
        /// </summary>
        /// <param name="longDateTime"></param>
        /// <returns></returns>
        public DateTime GetTime(string timeStamp)
        {
            DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            long lTime = long.Parse(timeStamp + "0000000");
            TimeSpan toNow = new TimeSpan(lTime);
            return dtStart.Add(toNow);
        }
    }
}
